﻿using Epm.AppLogic.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Buyer
{
    public partial class mfChatMessage : msBsc.Cls.mBPUser
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string userImg = "";//用户头像
        private bool isReceive = false;//是否存在聊天对象
        private DataSet getUserImg(string custId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister register = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                CDbParmCollection parmCollection = new CDbParmCollection();
                parmCollection.Add(new CDbParm("custId", ParameterDirection.Input, EDbType.String, 36, custId.Replace("'", "").Replace(",", "','")));
                DataSet ds = register.DataObj.QueryBySQL(@"SELECT CUSTOMER_DEFAULT_PIC,CUSTOMER_NICK FROM DJK_CUSTOMER_REGISTER WHERE CUSTOMER_REGISTER_ID =@custId", parmCollection);
                ds.Tables[0].TableName = register.TableName;
                //if (ds!=null&&ds.Tables[0].Rows.Count>0)
                //{
                //   return ds.Tables[0].Rows[0]["CUSTOMER_DEFAULT_PIC"].ToString();
                //}
                //return string.Empty;
                //register.DataObj.QueryBySQL("select "
                return ds;
            }
        }

        private bool getRceive()
        {
            string receiveId = this.GetParamHtValue("receiveId");
            DataSet ds = getUserImg(receiveId);
            if (ds != null && ds.Tables[0].Rows.Count < 1)
            {
                this.Alert("没有您交谈的对象,请重新选择");
                this.ExecJScript(@"window.location.href = '/';");
                return false;
            }
            string receiveName = this.GetParamHtValue("receiveName");//接收人
            if (receiveName != ds.Tables[0].Rows[0]["CUSTOMER_NICK"].ToString())
            {
                receiveName = ds.Tables[0].Rows[0]["CUSTOMER_NICK"].ToString();
                this.ExecJScript(@"$('#receiveName').val('" + receiveName + "');$('#h_title').text('与 " + receiveName + " 聊天中');");
            }
            return true;
        }




        private void sendMessage()
        {
            string message = this.GetParamHtValue("message");//消息内容
            if (string.IsNullOrEmpty(message))
            {
                this.Alert("消息内容不能为空");
                return;
            }
            if (message.Length > 200)
            {
                this.Alert("消息内容最大长度为200");
                return;
            }
            string receiveId = this.GetParamHtValue("receiveId");//接收人
            if (string.IsNullOrEmpty(receiveId))
            {
                this.Alert("接收人不能为空");
                return;
            }
            string receiveName = this.GetParamHtValue("receiveName");//接收人
            if (isReceive == false)
            {
                isReceive = getRceive();
            }
            getMessage();
            string sendId = msBsc.SvRef.UserInfo.Operator;//发送人
            using (msBsc.AppLogic.DataAlc.CBizChatMessage chatMessage = new AppLogic.DataAlc.CBizChatMessage())
            {
                string ret = chatMessage.SendMessage(sendId, msBsc.SvRef.UserInfo.OperatorName, receiveId, receiveName, message, "", false);
                if (ret != "OK")
                {
                    this.Alert(ret);
                }
                else
                {
                    if (string.IsNullOrEmpty(userImg))
                    {
                        DataSet ds = getUserImg(sendId);
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            //return ds.Tables[0].Rows[0]["CUSTOMER_DEFAULT_PIC"].ToString();
                            userImg = ds.Tables[0].Rows[0]["CUSTOMER_DEFAULT_PIC"].ToString();
                        }
                        else
                        {
                            userImg = "";
                        }
                        //return string.Empty;
                        //userImg = getUserImg();
                    }
                    this.ExecJScript(@"showSendMessage('" + message + "','" + userImg + "')");
                }
            }
        }

        private void getMessage()
        {
            string receiveId = this.GetParamHtValue("receiveId");//接收人
            if (string.IsNullOrEmpty(receiveId))
            {
                this.Alert("请选您的交流对象");
                return;
            }
            if (isReceive == false)
            {
                isReceive = getRceive();
            }
            string userId = msBsc.SvRef.UserInfo.Operator;
            using (msBsc.AppLogic.DataAlc.CBizChatMessage chatMessage = new AppLogic.DataAlc.CBizChatMessage())
            {
                DataSet ds = chatMessage.GetAllSenderMessage(receiveId, userId, "0", "0");
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                chatMessage.UpdateStatus(ds);
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                this.ExecJScript(@"showMessage(" + ret + ")");
            }
        }

        private void getHistoryMessage()
        {
            string receiveId = this.GetParamHtValue("receiveId");//接收人
            if (string.IsNullOrEmpty(receiveId))
            {
                this.Alert("接收人不能为空");
                return;
            }
            getMessage();
            string sendId = msBsc.SvRef.UserInfo.Operator;//发送人
            string number = this.GetParamHtValue("number");
            string rows = this.GetParamHtValue("rows");
            using (msBsc.AppLogic.DataAlc.CBizChatMessage chatMessage = new AppLogic.DataAlc.CBizChatMessage())
            {
                int pageIndex = 1,pageRows=20;
                int.TryParse(number, out pageIndex);
                int.TryParse(rows, out pageRows);
                DataTable dt = chatMessage.GetMessageRecord(receiveId, sendId, "0", pageIndex, pageRows);
                DataTable dtCopy = dt.Copy();
                DataView dv = dt.DefaultView;
                dv.Sort = "MESSAGE_SEND_TIME";
                dtCopy = dv.ToTable();

                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dtCopy, "", "", 1, 1000, "");
                this.ExecJScript(@"setHistory(" + ret + ")");
            }
        }


        public override void DoAction()
        {
            if (this.strAction == "sendMessage")
            {
                sendMessage();
            }
            else if (this.strAction == "getMessage")
            {
                getMessage();
            }
            else if (this.strAction == "getHistoryMessage")
            {
                getHistoryMessage();
            }
        }
    }
}